<!-- 批量智能分配 -->
<template>
  <glsx-dialog :show="value" title="批量智能分配" @close="closeDialog()">
    <div class="">
      <div class="title">已选中{{staticInfo.assignClueCnt}}条线索，其中主诉{{staticInfo.mainProjectClueCnt}}条，非主诉{{staticInfo.otherProjectClueCnt}}条</div>
      <div class="sub_title">当前可分配用户及规则(分配前请确认规则是否有误)：</div>
      <el-table class="el_table" :data="tabList" size="small" border max-height="360">
        <el-table-column label="所属部门" prop="departmentName" show-overflow-tooltip></el-table-column>
        <el-table-column label="姓名" prop="username" show-overflow-tooltip></el-table-column>
        <el-table-column label="排序" prop="assignOrder" show-overflow-tooltip></el-table-column>
        <el-table-column label="可分配城市" prop="assignCities" show-overflow-tooltip></el-table-column>
        <el-table-column label="线索增减比例" prop="indecreaseRatio" show-overflow-tooltip></el-table-column>
      </el-table>
    </div>
    <div slot="footer">
      <el-button type="primary" size="small" :loading="loading" @click="saveInfo">确认分配</el-button>
      <el-button type="default" size="small" @click="closeDialog()">取消</el-button>
    </div>
  </glsx-dialog>
</template>
<script>
import {
  SAVE_CLUE_SMARTASSIGN,
  GET_CLUE_ASSIGNCNT,
  GET_ASSIGNMENT_ASSIGNUSERS,
} from "@/http/clues";
import { showToast,showModal } from "@/common/util";
export default {
  name: "SmartAssign",
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    list: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      loading: false,
      staticInfo: {},
      tabList: [],
    };
  },
  mounted() {
    this.getList();
    this.getInfo();
  },
  methods: {
    // 关闭弹框
    closeDialog() {
      this.$emit("input", false);
    },

    // 智能分配
    saveInfo() {
      let _this = this;
      let list = this.list || [];
      let arr = [];
      list.map((k) => {
        arr.push(k.id);
      });
      _this.loading = true;
      SAVE_CLUE_SMARTASSIGN({ clueIds: arr }).then((res) => {
        _this.loading = false;
        let obj = res.data || {};
        if (res.code != 200) {
          return false;
        }

        _this.closeDialog();
        _this.$emit("change");

        if (obj.tips) {
          showModal({
            message: obj.tips,
            showCancelButton: true,
            type: "warning",
          });
          return false;
        }
        showToast({
          message: "分配成功",
          type: "success",
        });
      });
    },

    // 获取数量
    getInfo() {
      let _this = this;
      let list = this.list || [];
      let arr = [];
      list.map((k) => {
        arr.push(k.id);
      });
      GET_CLUE_ASSIGNCNT({ clueIds: arr }).then((res) => {
        _this.staticInfo = res.data || {};
      });
    },

    // 获取可分配人列表
    getList() {
      GET_ASSIGNMENT_ASSIGNUSERS().then((res) => {
        this.tabList = res.data || [];
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.title,
.sub_title {
  line-height: 2;
}
</style>